package com.sunny.ynhz.model;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.sunny.ynhz.api.HttpManager;
import com.sunny.ynhz.api.subscriber.ApiSubscriber;
import com.sunny.ynhz.bean.PatrolRiverTime;
import com.sunny.ynhz.bean.ProblemTypeDataBean;
import com.sunny.ynhz.ui.MainActivity;
import com.sunny.ynhz.utils.SPUtil;
import com.sunny.ynhz.utils.TypeAndStatusUtil;

import org.json.JSONObject;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by huhuan123 on 2018/7/19.
 *
 * @describe TODO
 */

public class ProblemTypeModel {
    Context context;
    IProblemTypeResultView iProblemTypeResultView;

    public ProblemTypeModel(Context context, IProblemTypeResultView iProblemTypeResultView) {
        this.context = context;
        this.iProblemTypeResultView = iProblemTypeResultView;
    }

    public void getProblemTypeData() {
        if ((!(context instanceof MainActivity)) && TypeAndStatusUtil.getProblemTpye().size() > 0) {
            iProblemTypeResultView.onCache();
            return;
        } else {
            TypeAndStatusUtil.getProblemTpye().clear();
        }

        HttpManager.getInstance().getProblemTypeDataBean("problem_deadline").subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new ApiSubscriber(context, new ApiSubscriber.HttpOnResultListener<ProblemTypeDataBean>() {
            @Override
            public void onNext(ProblemTypeDataBean problemTypeDataBean) {
                TypeAndStatusUtil.setProblemTpye(problemTypeDataBean);
                Gson gson = new Gson();
                String gsonStr = gson.toJson(problemTypeDataBean);
                SPUtil.put("problem_deadline", gsonStr);
                if (iProblemTypeResultView != null) {
                    iProblemTypeResultView.onSuccess(problemTypeDataBean);
                }

            }

            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                if (iProblemTypeResultView != null) {
                    String problem_deadline = SPUtil.get("problem_deadline", "").toString();
                    if (!TextUtils.isEmpty(problem_deadline)) {
                        Gson gson = new Gson();
                        ProblemTypeDataBean problemTypeDataBean = gson.fromJson(problem_deadline, ProblemTypeDataBean.class);
                        TypeAndStatusUtil.setProblemTpye(problemTypeDataBean);
                        iProblemTypeResultView.onSuccess(problemTypeDataBean);

                    } else {
                        iProblemTypeResultView.onFail();
                    }
                }

            }

            @Override
            public void onstart() {
                if (iProblemTypeResultView != null) {
                    iProblemTypeResultView.onStartLoad();
                }
            }
        }));

    }

    public void getPatrolRiver() {
        String userId = SPUtil.get("userId", "").toString();
        HttpManager.getInstance().getPatrolRiver("patrol_time").subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new ApiSubscriber(context, new ApiSubscriber.HttpOnResultListener<PatrolRiverTime>() {
            @Override
            public void onNext(PatrolRiverTime patrolRiverTime) {
                PatrolRiverTime.DateBean dateBean = patrolRiverTime.getDate().get(0);
                String value = dateBean.getLabel();
                SPUtil.put(userId + "patrol_time", value);
            }

            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
            }

            @Override
            public void onstart() {
            }
        }));

    }
}
